using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pacocation
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Bola bola;
        Player player;
        Colisao colisao;
        Obstaculo obstaculo;
        fundo Fundo;
        Pontuacao Rpontuacao;
        Tempo tempo;
        Tempo tempo2;
        //double tempo2 = 0;
        int conta = 0;
        Random rndSorteio;
        float tempoContador;

        bool rodar = false;
        KeyboardState kState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            kState = new KeyboardState();
            
            IsMouseVisible = true;
           
            
        }
        protected override void Initialize()
        {
          bola = new Bola(Content);
          player = new Player(Content);
          colisao = new Colisao();
          obstaculo = new Obstaculo(Content);
          Fundo = new fundo(Content);
          Rpontuacao = new Pontuacao (Content);
          tempo = new Tempo(Content);
          tempo2 = new Tempo(Content);


          graphics.PreferredBackBufferWidth = 800;
          graphics.PreferredBackBufferHeight = 600;

          graphics.ApplyChanges();

            base.Initialize();

            obstaculo.DesenhaObstaculos();
            obstaculo.PodeDescer = false;
        }
        protected override void LoadContent()
        {
           
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
          
        }
        protected override void UnloadContent()
        {
            
        }
        protected override void Update(GameTime gameTime)
        {
          
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            kState = Keyboard.GetState();
            base.Update(gameTime);
            obstaculo.Update(gameTime);

            tempo.minutos = gameTime.TotalGameTime.Minutes;
            tempo.segundos = gameTime.TotalGameTime.Seconds;

            //tempo2.segundos = gameTime.ElapsedGameTime.Seconds;

            tempoContador += 1;

            if (tempoContador >= 200)
            {
                for (int i = 0; i < obstaculo.listRects.Count; i++)
                {
                    obstaculo.listRects[i] = new Rectangle(obstaculo.listRects[i].X, obstaculo.listRects[i].Y + 10,
                                             obstaculo.listRects[i].Width, obstaculo.listRects[i].Height);
                }

                tempoContador = 0;
            }

            //if (obstaculo.PodeDescer)
            //{

            //   // while (conta <= obstaculo.listRects.Count)
            //  //  {
            //        for (int i = 0; i < obstaculo.listRects.Count; i++)
            //        {
            //            obstaculo.listRects[i] = new Rectangle(obstaculo.listRects[i].X, obstaculo.listRects[i].Y + 10,
            //                                        obstaculo.listRects[i].Width, obstaculo.listRects[i].Height);

            //            //conta = conta + 1;

            //        }
                    
            //        obstaculo.PodeDescer = false;
            //       //conta = 0;
                    
            //   // }

            //}
            if (kState.IsKeyDown(Keys.Space) && rodar == false)
            {
                rodar = true;
            }



            if (bola.rectBola.Y >= (600 - 25))
            {
                rodar = false;
                bola.rectBola.Y = 525;
                bola.rectBola.X = 505;
                player.rectPlayer.Y = 550;
                player.rectPlayer.X = 480;
            }
            if (rodar == false)
            {
                if (kState.IsKeyDown(Keys.Right) && player.rectPlayer.X <= (800 - 85))
                {
                    bola.rectBola.X+=5;
                    player.rectPlayer.X+= 5;
                }

                if (kState.IsKeyDown(Keys.Left) && player.rectPlayer.X >= 240)
                {
                    bola.rectBola.X-= 5;
                    player.rectPlayer.X-= 5;
                }
            }

            if (rodar == true)
            {
                bola.Update(gameTime);
                player.Update(gameTime);

                if (colisao.VerificaColisao(bola.rectBola, player.rectPlayer))
                {
                    if (bola.posicaox == 5)
                    {
                        bola.posicaox = 5;
                    }
                    else
                    {
                        bola.posicaox = -5;
                    }
                    bola.posicaoy = 5;
                }




                for (int i = 0; i < obstaculo.listRects.Count; i++)

                    if (colisao.VerificaColisao(bola.rectBola, obstaculo.listRects[i]))
                    {


                        if (bola.rectBola.Y < (obstaculo.listRects[i].Y + obstaculo.listRects[i].Height / 2))
                        {
                            bola.posicaoy = 5;

                            if (bola.rectBola.X < (obstaculo.listRects[i].X + obstaculo.listRects[i].Width / 2))
                            {
                                bola.posicaox = 5;
                            }
                            else
                            {
                                bola.posicaox = -5;
                            }

                            obstaculo.listRects.RemoveAt(i);
                            Rpontuacao.pontos++;

                        }
                        else
                        {
                            bola.posicaoy = -5;

                            if (bola.rectBola.X < (obstaculo.listRects[i].X + obstaculo.listRects[i].Width / 2))
                            {
                                bola.posicaox = 5;
                            }
                            else
                            {
                                bola.posicaox = -5;
                            }

                            obstaculo.listRects.RemoveAt(i);

                            Rpontuacao.pontos++;

                        }
                    }
            }
            
            }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);

            
            Fundo.Draw(spriteBatch);
            bola.Draw(spriteBatch);
            player.Draw(spriteBatch);
            obstaculo.Draw(spriteBatch);
            Rpontuacao.Draw(spriteBatch);
            tempo.Draw(spriteBatch);
        }
    }
}
